export function dateTime2Str(date, flag) {
  var year = date.getFullYear();
  var month = (date.getMonth() + 1).toString();
  var day = (date.getDate()).toString();
  var hour = date.getHours().toString();
  var minute = date.getMinutes().toString();
  var sec = date.getSeconds().toString();
  if (month.length == 1) {
    month = '0' + month;
  }
  if (day.length == 1) {
    day = '0' + day;
  }
  if (hour.length == 1) hour = '0' + hour;
  if (minute.length == 1) minute = '0' + minute;
  if (sec.length == 1) sec = '0' + sec;
  var dateTime = '';
  if (flag == 1) dateTime = year + '-' + month + '-' + day;
  else if (flag == 2) dateTime = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + sec;
  else if (flag == 3) dateTime = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
  return dateTime;
}

export function sortByKey(array, key, type, asce) { //对象数组排序：关键字为可比大小的简单类型
  return array.sort(function (a, b) {
    var x = a[key];
    var y = b[key];

    var res = 0;
    if (type === 'int') {
      x = parseInt(x);
      if (!isRealNum(x)) x = -99;
      y = parseInt(y);
      if (!isRealNum(y)) y = -99;
      res = (x < y) ? -1 : ((x > y) ? 1 : 0);
      // console.log(x,y);    
    } else {
      // console.log(x,y);      
      if (x != null && y == null) res = 1;
      else if (x == null && y != null) res = -1;
      else if (x != null && y != null) {
        res = ((x < y) ? -1 : ((x > y) ? 1 : 0));
      }
    }

    if (asce) return res;
    else return -res;
  });
}

export function isRealNum(val) {
  if (val === '' || val == null) return false;
  if (!isNaN(val)) return true;
  return false;
}

export function isMobile(val) {
  return /^1\d{10}$/.test(val);
}

export function isUserNo(val) {
  return /^\d{8}$/.test(val);
}

export function getOrder(state, order, alikeOrder, isLast) {
  if (order > 0) return order + '面 - ';
  else if (isLast && state != 0) return '过期 - ';
  else if (alikeOrder > 0) return '同场 - ';
  else if (state > 1 && state < 40) return '无效 - ';
  else return '? - ';
}
export function viewMeState(mestate, recstate) { //api端有1份，修改时需要同时
  switch (mestate) {
  case 0:
    return '删除';
  case 1:
  case -11:
    var state = '正常';
    switch (recstate) {
    case 11:
      state = '录用';
      break;
    case 12:
      state = '暂停';
      break;
    case 13:
      state = '成功';
      break;
    }
    return state;
  case 2:
    return '待审';
  case 10:
    return '散客';
  case 11:
    return '游客';
  case 12:
    return '失效';
  case 13:
    return '失效';
  case 19:
    return '失信';
  case -6:
    return '天使简历';
  default:
    return '未知';
  }
}
export function viewMeStateId(mestate, recstate) { //api端有1份，修改时需要同时
  switch (mestate) {
  case 0:
    return 0; //'删除';
  case 1:
    var state = 1; //'正常';
    switch (recstate) {
    case 11:
      state = 11; //'录用';break;
      break;
    case 12:
      state = 12; //'暂停';break;
      break;
    case 13:
      state = 13; //'成功';break;
      break;
    }
    return state;
  case 2:
    return 2; //'待审';
  case 11:
    return 3; //'游客';
  case 12:
    return 4; //'失效';
  case 13:
    return 4; //'失效';
  case 19:
    return 5; //'失信';
  default:
    return 9; //'未知';
  }
}

export function viewHrState(mestate) {
  switch (mestate) {
  case 0:
    return '删除';
  case 1:
    return '正常';
  case 2:
    return '离职';
  case 4:
    return '待审';
  case 9:
    return '失信';
  default:
    return '未知';
  }
}
export function viewSex(val) {
  switch (val) {
  case 1:
    return '男';
  case 2:
    return '女';
  default:
    return '未知';
  }
}

export function TimeDowm(TimeStamp, returnVal) //第2个参数：返回计时间隔秒
{
  let nowTime = new Date();
  let endTime = new Date(TimeStamp);
  let t = endTime.getTime() - nowTime.getTime();
  if (t > 0) {
    let day = Math.floor(t / 86400000);
    let hour = Math.floor((t / 3600000) % 24);
    let min = Math.floor((t / 60000) % 60);
    let sec = Math.floor((t / 1000) % 60);
    let format = '';
    if (day > 0) {
      format = `${day}天${hour}小时`; //${min}分${sec}秒`;
      returnVal[0] = 3600;
    } else if (hour > 0) {
      format = `${hour}小时${min}分`; //${sec}秒;
      returnVal[0] = 60;
    } else if (min > 0) {
      format = `${min}分${sec}秒`;
      returnVal[0] = 1;
    } else if (sec > 0) {
      format = `${sec}秒`;
      returnVal[0] = 1;
    }
    return format;
  }
  return 'refresh';
}

import request from '@/utils/request';
export function getAPIServerTime() //取API服务当前日期时间
{
  return request({
    url: '/Common/GetAPIServerTime',
    method: 'get',
  });
}
